package com.mytest.example.asyncservlet.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Slf4j
public class AsyncTaskInterceptor implements AsyncHandlerInterceptor {

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拦截异步请求的方式

        log.info("线程ID={},Name={},afterConcurrentHandlingStarted", Thread.currentThread().getId(), Thread.currentThread().getName());
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("线程ID={},Name={},preHandle", Thread.currentThread().getId(), Thread.currentThread().getName());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("线程ID={},Name={},postHandle", Thread.currentThread().getId(), Thread.currentThread().getName());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("线程ID={},Name={},afterCompletion", Thread.currentThread().getId(), Thread.currentThread().getName());
    }
}
